//
//  OMOrderRefundListViewController.m
//  OMENGO_NEW
//
//  Created by 梁尚嘉 on 2017/5/22.
//  Copyright © 2017年 Kamfat. All rights reserved.
//

#import "OMOrderRefundListViewController.h"
#import "OMRefund.h"
#import "OMOrder.h"
#import "OMOrderRefundListTableCell.h"
@interface OMOrderRefundListViewController ()
@property (nonatomic, strong) NSMutableArray <OMRefund*>*refunds;
@property (nonatomic, assign) NSUInteger pageNumber;
@property (nonatomic, assign) NSUInteger pageSize;
@property (nonatomic, strong) NSMutableDictionary *cellHeightDic;
@end

@implementation OMOrderRefundListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.tableView  registerClass:[OMOrderRefundListTableCell class] forCellReuseIdentifier:[OMOrderRefundListTableCell cellIdentifier]];
    [self setupUI];
    self.refunds = [NSMutableArray array];
    self.cellHeightDic = [NSMutableDictionary dictionaryWithCapacity:0];
    [self getRefundList];
}

- (void)setupUI
{
    self.title = @"我的退款";
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    self.tableView.sectionFooterHeight = DBL_EPSILON;
    self.tableView.sectionHeaderHeight = SM_SIZE(20);
    self.tableView.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SM_SCREEN_WIDTH, SM_SIZE(20))];

    @weakify(self);
    [self.tableView.kt_header addRefreshWithBlock:^{
        @strongify(self);
        self.pageNumber = 0;
        [self getRefundList];
    }];

    //分页
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        @strongify(self);
        self.pageNumber++;
        [self getRefundList];
    }];
    footer.stateLabel.font = SM_CFont(24);
    footer.stateLabel.textColor = SM_darkGrayColor;
    footer.triggerAutomaticallyRefreshPercent = SM_SIZE(-30);
    footer.automaticallyHidden = true;
    [footer setTitle:@"没有更多退款了呢" forState:MJRefreshStateNoMoreData];
    self.tableView.mj_footer = footer;

    self.emptyImage = [UIImage imageNamed:@"ic_order_refund_empty"];
    self.emptyDataMessage = @"您还没有退款哦";
}

- (void)reloadDataFromNetWork
{
    [self getRefundList];
}

- (void)getRefundList
{
    [self.task cancel];
    [self startAnimating:YES];
    self.task=
    [KTHttpUtils GET_NOCACHE:@"member/refunds/list.jhtml"
                  parameters:@{@"pageNumber":@(self.pageNumber),@"pageSize":@(self.pageSize)}
                    progress:nil
                  completion:^(id responseObject, BOOL isSuccess, NSString *message) {

                      if (isSuccess) {

                          OMRefundListResponse *response = [OMRefundListResponse mj_objectWithKeyValues:responseObject[OMDatumKey]];
                          for (OMRefund *refund in response.list) {
                              [refund.order desc];
                          }
                          if (!self.tableView.mj_footer.isRefreshing) {
                              [self.refunds removeAllObjects];
                          }
                          NSUInteger index = self.refunds.count;
                          [self.refunds addObjectsFromArray:response.list];
                          for (NSUInteger i = index; i < self.refunds.count; i ++) {
                              CGFloat height = [OMOrderRefundListTableCell cellHeightWithObj:self.refunds[i]];
                              self.cellHeightDic[@(i)] = @(height);
                          }
                          [self startAnimating:NO];
                          [self.tableView reloadData];

                          response.totalPage == self.pageNumber?[self.tableView.mj_footer endRefreshingWithNoMoreData]:[self.tableView.mj_footer endRefreshing];

                          [self.tableView endRefreshingSuccess];
                      }else {
                          if (self.tableView.mj_footer.isRefreshing) {
                              [self.tableView.mj_footer endRefreshing];
                              self.pageNumber --;
                          }
                          [self showNetWorkError];
                          [self.tableView endRefreshingFail];
                      }
                  }];
}


#pragma mark - Tableview delegate && datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.refunds.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    OMOrderRefundListTableCell *cell = [OMOrderRefundListTableCell tableViewCellWithTableView:tableView];
    [cell configureCellWithObj:self.refunds[indexPath.section]];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [self.cellHeightDic[@(indexPath.section)] floatValue];
//    [tableView fd_heightForCellWithIdentifier:[OMOrderRefundListTableCell cellIdentifier] configuration:^(OMOrderRefundListTableCell *cell) {
//        [cell configureCellWithObj:self.refunds[indexPath.section]];
//    }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NaviPush(@"OMOrderRefundDetailViewController", @{@"refundId":self.refunds[indexPath.section].id});
}

- (NSUInteger)pageSize
{
    if (!_pageSize) {
        _pageSize = 20;
    }
    return _pageSize;
}

- (NSUInteger)pageNumber
{
    if (!_pageNumber) {
        _pageNumber = 1;
    }
    return _pageNumber;
}

@end
